package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.ProductBOInterface;
import org.cuatrovientos.erp.model.Product;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Product management
 * 
 * @author VB
 */
@Controller
public class ProductController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(ProductController.class);

	@Autowired
	private ProductBOInterface productBO;

	/**
	 * default constructor
	 */
	public ProductController() {
		logger.info("Product BEAN instantiated");
	}

	/**
	 * handles default /product or /product/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/product","/product/list" )
	 */
	@RequestMapping({ "/product", "/product/list" })
	public ModelAndView productDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Product> productList = productBO.selectAll();
		model.put("products", productList);
		return new ModelAndView("ProductList", model);
	}

	/**
	 * handles /product/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/product/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/product/search" })
	public ModelAndView searchByName(@RequestParam(value = "name", defaultValue = "", required = true) String name) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Product> productList = productBO.searchByName(name);
		model.put("products", productList);

		return new ModelAndView("ProductList", model);
	}
	
	/**
	 * handles /product/search request, redirected to default page
	 * 
	 * @param model
	 * @return the description of the view to show
	 * @RequestMapping({"/product/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/product/search" })
	public ModelAndView searchByDescription(@RequestParam(value = "description", defaultValue = "", required = true) String description) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Product> productList = productBO.searchByDescription(description);
		model.put("products", productList);

		return new ModelAndView("ProductList", model);
	}

	/**
	 * handles delete product, and redirect to product default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/product","/product/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/product/delete" })
	public String deleteProduct(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		productBO.deleteById(id);
		return "redirect:/product";
	}

	/**
	 * handles new product request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "product"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/product/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/product/new" })
	public ModelAndView newProduct() {
		logger.info("new product ");
		Product product = new Product();

		return new ModelAndView("NewProduct", "product", product);
	}

	/**
	 * handles new product request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/product/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/product/save" })
	public String newProduct(@Valid Product product,BindingResult bindingResult) {
		logger.info("new product " + product.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("product", product);
			return "NewProduct";
		}

		// If data is ok, insert and go on
		productBO.insert(product);
		return "redirect:/product";
	}

	/**
	 * handles delete product, and redirect to product default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/product","/product/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/product/detail" })
	public ModelAndView productDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Product product = productBO.selectById(id);
		model.put("product", product);

		return new ModelAndView("ProductDetail", model);
	}

	/**
	 * handles product update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/product/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/product/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Product product = productBO.selectById(id);
		model.put("product", product);
		return new ModelAndView("UpdateProduct", model);

	}

	/**
	 * handles save update product request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/product/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/product/saveupdate" })
	public String saveUpdate(@Valid Product product,BindingResult bindingResult) {
		logger.info("update product " + product.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("product", product);
			return "UpdateProduct";
		}

		productBO.update(product);
		return "redirect:/product";
	}
}
